//
//  LoginViewController.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/5.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "LoginViewController.h"
#import "FindPasswordViewController.h"
#import "RegitsterViewController.h"
@interface LoginViewController ()

@property (nonatomic, strong) UIButton *userNameLoginBtn;  // 用户名登录
@property (nonatomic, strong) UIButton *phoneLoginBtn;  // 手机短信登录
@property (nonatomic, strong) UIImageView *transformImg;
@property (nonatomic, strong) UITextField *userNameTextF; // 用户名
@property (nonatomic, strong) UITextField *passwordTextF;  // 密码
@property (nonatomic, strong) UITextField *phoneNumTextF;  // 手机号
@property (nonatomic, strong) UITextField *verificationCodeTextF;  // 验证码
@property (nonatomic, strong) UIButton *verificationBtn;  // 验证码
@property (nonatomic, assign) NSInteger loginType;  // 验证码 1用户名登录  2手机短信登录
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    self.loginType = 1;
}

- (void)backBtnClick
{
    [self pop];
}

- (void)setupUI
{
    UIImageView *bgImg = [[UIImageView alloc] init];
    bgImg.image = [UIImage imageNamed:@"loginBGImg"];
    bgImg.userInteractionEnabled = YES;
    [self.view addSubview:bgImg];
    [bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.hitEdgeInsets = UIEdgeInsetsMake(-15, -15, -15, -15);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"whiteBack"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(59);
        make.left.offset(26);
        make.width.height.mas_equalTo(25);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"用户登录";
    titleLabel.font = JSS_MEDIUM_FONT(34);
    titleLabel.textColor = RGB_VALUE(0xFFFFFF);
    [bgImg addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(107);
        make.left.offset(35);
        make.right.offset(-35);
        make.height.mas_equalTo(48);
    }];
    
    self.userNameLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.userNameLoginBtn setTitle:@"用户名登录" forState:UIControlStateNormal];
    [self.userNameLoginBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [self.userNameLoginBtn.titleLabel setFont:JSS_MEDIUM_FONT_7];
    [self.userNameLoginBtn addTarget:self action:@selector(userNameLoginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:self.userNameLoginBtn];
    [self.userNameLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(26);
        make.left.offset(38);
        make.width.mas_equalTo(85);
        make.height.mas_equalTo(24);
    }];
    
    self.phoneLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.phoneLoginBtn setTitle:@"手机短信登录" forState:UIControlStateNormal];
    [self.phoneLoginBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [self.phoneLoginBtn.titleLabel setFont:JSS_MEDIUM_FONT_7];
    [self.phoneLoginBtn addTarget:self action:@selector(phoneLoginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:self.phoneLoginBtn];
    [self.phoneLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userNameLoginBtn.mas_right).offset(24);
        make.centerY.equalTo(self.userNameLoginBtn);
        make.width.mas_equalTo(110);
        make.height.mas_equalTo(24);
    }];
    
    self.transformImg = [[UIImageView alloc] init];
    self.transformImg.image = [UIImage imageNamed:@"loginBottomView"];
    [bgImg addSubview:self.transformImg];
    [self.transformImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(35);
        make.top.equalTo(self.userNameLoginBtn.mas_bottom).offset(4);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(4);
    }];
    
    self.userNameTextF = [InsetsTextField new];
    NSMutableAttributedString *nameplaceholderString = [[NSMutableAttributedString alloc] initWithString:@"用户名" attributes:@{NSForegroundColorAttributeName : RGB_VALUE(0xA5A5A5)}];
    self.userNameTextF.attributedPlaceholder = nameplaceholderString;
    self.userNameTextF.font = JSS_MEDIUM_FONT_7;
//    self.userNameTextF.textColor = RGB_VALUE(0xA5A5A5);
    self.userNameTextF.layer.masksToBounds = YES;
    self.userNameTextF.layer.cornerRadius = 14;
    self.userNameTextF.backgroundColor = UIColor.whiteColor;
    [bgImg addSubview:self.userNameTextF];
    [self.userNameTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.equalTo(self.phoneLoginBtn.mas_bottom).offset(54);
        make.right.offset(-28);
        make.height.mas_equalTo(56);
    }];
    
    self.phoneNumTextF = [InsetsTextField new];
//    self.phoneNumTextF.placeholder = @"手机号";
    NSMutableAttributedString *phoneplaceholderString = [[NSMutableAttributedString alloc] initWithString:@"手机号" attributes:@{NSForegroundColorAttributeName : RGB_VALUE(0xA5A5A5)}];
    self.phoneNumTextF.attributedPlaceholder = phoneplaceholderString;
    self.phoneNumTextF.font = JSS_MEDIUM_FONT_7;
//    self.phoneNumTextF.textColor = RGB_VALUE(0xA5A5A5);
    self.phoneNumTextF.layer.masksToBounds = YES;
    self.phoneNumTextF.layer.cornerRadius = 14;
    self.phoneNumTextF.backgroundColor = UIColor.whiteColor;
    [bgImg addSubview:self.phoneNumTextF];
    [self.phoneNumTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.equalTo(self.phoneLoginBtn.mas_bottom).offset(54);
        make.right.offset(-147);
        make.height.mas_equalTo(56);
    }];
    
    self.verificationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.verificationBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.verificationBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [self.verificationBtn.titleLabel setFont:JSS_MEDIUM_FONT_7];
    self.verificationBtn.layer.masksToBounds = YES;
    self.verificationBtn.layer.cornerRadius = 14;
    self.verificationBtn.backgroundColor = RGB_VALUE(0xFFB100);
    [self.verificationBtn addTarget:self action:@selector(verificationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:self.verificationBtn];
    [self.verificationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneNumTextF.mas_right).offset(6);
        make.centerY.equalTo(self.phoneNumTextF);
        make.right.offset(-28);
        make.height.equalTo(self.phoneNumTextF);
    }];
    
    self.passwordTextF = [InsetsTextField new];
    NSMutableAttributedString *passwordplaceholderString = [[NSMutableAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName : RGB_VALUE(0xA5A5A5)}];
    self.passwordTextF.attributedPlaceholder = passwordplaceholderString;
//    self.passwordTextF.placeholder = @"密码";
    self.passwordTextF.font = JSS_MEDIUM_FONT_7;
//    self.passwordTextF.textColor = RGB_VALUE(0xA5A5A5);
    self.passwordTextF.layer.masksToBounds = YES;
    self.passwordTextF.layer.cornerRadius = 14;
    self.passwordTextF.backgroundColor = UIColor.whiteColor;
    self.passwordTextF.secureTextEntry = YES;
    [bgImg addSubview:self.passwordTextF];
    [self.passwordTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.equalTo(self.userNameTextF.mas_bottom).offset(10);
        make.right.offset(-28);
        make.height.mas_equalTo(56);
    }];
    
    self.verificationCodeTextF = [InsetsTextField new];
    NSMutableAttributedString *verificationCodeplaceholderString = [[NSMutableAttributedString alloc] initWithString:@"验证码" attributes:@{NSForegroundColorAttributeName : RGB_VALUE(0xA5A5A5)}];
    self.verificationCodeTextF.attributedPlaceholder = verificationCodeplaceholderString;
//    self.verificationCodeTextF.placeholder = @"验证码";
    self.verificationCodeTextF.font = JSS_MEDIUM_FONT_7;
//    self.verificationCodeTextF.textColor = RGB_VALUE(0xA5A5A5);
    self.verificationCodeTextF.layer.masksToBounds = YES;
    self.verificationCodeTextF.layer.cornerRadius = 14;
    self.verificationCodeTextF.backgroundColor = UIColor.whiteColor;
    [bgImg addSubview:self.verificationCodeTextF];
    [self.verificationCodeTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.equalTo(self.userNameTextF.mas_bottom).offset(10);
        make.right.offset(-28);
        make.height.mas_equalTo(56);
    }];
    self.verificationCodeTextF.hidden = YES;
    self.verificationBtn.hidden = YES;
    self.phoneNumTextF.hidden = YES;
    
    UIButton *findPasswordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [findPasswordBtn setTitle:@"忘记密码？点此找回" forState:UIControlStateNormal];
    [findPasswordBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [findPasswordBtn.titleLabel setFont:JSS_MEDIUM_FONT_7];
    [findPasswordBtn addTarget:self action:@selector(findPasswordBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:findPasswordBtn];
    [findPasswordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordTextF.mas_bottom).offset(10);
        make.right.offset(-37);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(24);
    }];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [loginBtn.titleLabel setFont:JSS_MEDIUM_FONT_7];
    loginBtn.backgroundColor = RGB_VALUE(0xDC0000);
    loginBtn.layer.masksToBounds = YES;
    loginBtn.layer.cornerRadius = 14;
    [loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(findPasswordBtn.mas_bottom).offset(16);
        make.right.offset(-28);
        make.left.offset(28);
        make.height.mas_equalTo(56);
    }];
    
    UIButton *bottomLabel = [UIButton buttonWithType:UIButtonTypeCustom];
    [bottomLabel setTitle:@"使用快捷登录 或 免费注册" forState:UIControlStateNormal];
    [bottomLabel setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [bottomLabel.titleLabel setFont:JSS_MEDIUM_FONT_7];
    [bottomLabel addTarget:self action:@selector(bottomLabelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(26);
        make.centerX.offset(0);
        make.height.mas_equalTo(24);
        make.width.mas_equalTo(202);
    }];
    
    UIButton *wechatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [wechatBtn setImage:[UIImage imageNamed:@"wechat"] forState:UIControlStateNormal];
    [wechatBtn addTarget:self action:@selector(wechatBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:wechatBtn];
    [wechatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomLabel.mas_bottom).offset(33);
        make.left.offset(117);
        make.width.height.mas_equalTo(60);
    }];
    
    UIButton *alipayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [alipayBtn setImage:[UIImage imageNamed:@"alipay"] forState:UIControlStateNormal];
    [alipayBtn addTarget:self action:@selector(alipayBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:alipayBtn];
    [alipayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wechatBtn.mas_top);
        make.right.offset(-117);
        make.width.height.mas_equalTo(60);
    }];
}

// 用户名登录
- (void)userNameLoginBtnClick:(UIButton *)sender
{
    self.loginType = 1;
    self.verificationCodeTextF.hidden = YES;
    self.verificationBtn.hidden = YES;
    self.phoneNumTextF.hidden = YES;
    self.userNameTextF.hidden = NO;
    self.passwordTextF.hidden = NO;
    [self.transformImg mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.offset(35);
    }];
}

// 手机短信登录
- (void)phoneLoginBtnClick:(UIButton *)sender
{
    self.loginType = 2;
    self.verificationCodeTextF.hidden = NO;
    self.verificationBtn.hidden = NO;
    self.phoneNumTextF.hidden = NO;
    self.userNameTextF.hidden = YES;
    self.passwordTextF.hidden = YES;
    
    [self.transformImg mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.offset(157);
    }];
}

// 获取验证码
- (void)verificationBtnClick:(UIButton *)sender
{
    if (self.loginType == 2) { // 用户名登录
        if (NULLString(self.phoneNumTextF.text)) {
            [self showMessage:@"手机号不能为空"];
            return;
        }
    }
    [self.view endEditing:YES];
    NSDictionary *paramDic = @{@"mobile":self.phoneNumTextF.text};
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"identify" parameters:paramDic done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSTimeInterval seconds =120;
        NSDate *endTime = [NSDate dateWithTimeIntervalSinceNow:seconds];// 最后期限
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
        dispatch_source_t _sometimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, queue);
        dispatch_source_set_timer(_sometimer, dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC,0);// 每秒执行一次
        dispatch_source_set_event_handler(_sometimer, ^{
            int interval = [endTime timeIntervalSinceNow];
            if(interval >0) {// 更新倒计时
                NSString *timeStr = [NSString stringWithFormat:@"%d秒内有效", interval];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [sender setTitle:timeStr forState:UIControlStateNormal];                     sender.userInteractionEnabled =NO;
                    [sender setTitleColor:RGB_VALUE(0xA5A5A5) forState:UIControlStateNormal];
                });
            }else{
                // 倒计时结束，关闭
                
                dispatch_source_cancel(_sometimer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [sender setTitle:@"获取验证码"forState:UIControlStateNormal];
                    [sender setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];                sender.userInteractionEnabled =YES;
                });
            }
        });
        dispatch_resume(_sometimer);
        
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
    
}

// 忘记密码找回
- (void)findPasswordBtnClick:(UIButton *)sender
{
    [self pushVc:[[FindPasswordViewController alloc]init]];
}

//
- (void)loginBtnClick:(UIButton *)sender
{
    if (self.loginType == 1) { // 用户名登录
        if (NULLString(self.userNameTextF.text) || NULLString(self.passwordTextF.text)) {
            [self showMessage:@"用户名或密码不能为空"];
            return;
        }
    } else { // 验证码登录
        if (NULLString(self.phoneNumTextF.text) || NULLString(self.verificationCodeTextF.text)) {
            [self showMessage:@"用户名或密码不能为空"];
            return;
        }
    }
    
    NSDictionary *paramDic = @{
        @"login_type":@(self.loginType),
        @"mobile":NULLString(self.phoneNumTextF.text)?@"":self.phoneNumTextF.text,
//        @"username":@"13333333333",
//        @"password":@"111111",
        @"username":NULLString(self.userNameTextF.text)?@"":self.userNameTextF.text,
        @"password":NULLString(self.passwordTextF.text)?@"":self.passwordTextF.text,
        @"code":NULLString(self.verificationCodeTextF.text)?@"":self.verificationCodeTextF.text
    };
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypePost URLString:@"login" parameters:paramDic done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        [[DVUserManager shareManager] updateUserInfo:responseObject[@"data"]];
        NSDictionary *userInfo = [DVUserManager shareManager].userInfo;
        NSLog(@"登录成功之后的个人token%@",userInfo);
//        DVUserManager *manager = [DVUserManager shareManager];
//        [manager setRootViewController:manager.tabBarController];
        [self pop];
        
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
    
//    [[DVUserManager shareManager] updateUserInfo:dict[@"result"]];
//    DVUserManager *manager = [DVUserManager shareManager];
//    [manager setRootViewController:manager.tabBarController];
}

///  注册
- (void)bottomLabelBtnClick:(UIButton *)sender
{
    [self pushVc:[[RegitsterViewController alloc]init]];
}

// 微信登录
- (void)wechatBtnClick:(UIButton *)sender
{
    
}

// 支付宝登录
- (void)alipayBtnClick:(UIButton *)sender
{
    
}
@end
